use super::models::Course;
use super::state::AppState;

use actix_web::{web, HttpResponse};
use chrono::Utc;

pub async fn health_check_handler(
  app_state: web::Data<AppState>,
) -> HttpResponse {
  let health_check_response = &app_state.health_check_response;
  let mut visit_count = app_state.visit_count.lock().unwrap();

  let response = format!("{} {} times.", health_check_response, visit_count);

  *visit_count += 1;

  HttpResponse::Ok().json(&response)
}

pub async fn new_course(
  new_course: web::Json<Course>,
  app_state: web::Data<AppState>,
) -> HttpResponse {
  let course_count = app_state
    .courses
    .lock()
    .unwrap()
    .clone()
    .into_iter()
    .filter(|course| course.teacher_id == new_course.teacher_id)
    .collect::<Vec<Course>>()
    .len();

  let new_course = Course {
    teacher_id: new_course.teacher_id,
    id: Some(course_count + 1),
    name: new_course.name.clone(),
    time: Some(Utc::now().naive_utc()),
  };

  app_state.courses.lock().unwrap().push(new_course);
  HttpResponse::Ok().json("Successed.")
}

pub async fn get_courses_for_teacher(
  app_state: web::Data<AppState>,
  params: web::Path<(usize,)>, // 这里注意写法 (usize,) 有逗号
) -> HttpResponse {
  let teacher_id: usize = params.0;

  let filtered_courses = app_state
    .courses
    .lock()
    .unwrap()
    .clone()
    .into_iter()
    .filter(|course| course.teacher_id == teacher_id)
    .collect::<Vec<Course>>();
  if filtered_courses.len() > 0 {
    HttpResponse::Ok().json(filtered_courses)
  } else {
    HttpResponse::Ok().json("No Data.")
  }
}

pub async fn get_course_detail(
  app_state: web::Data<AppState>,
  params: web::Path<(usize, usize)>, // 这里注意写法
) -> HttpResponse {
  // let (user_id, teacher_id) = params.0;
  let teacher_id = params.0;
  let course_id = params.1;

  let selected_course = app_state
    .courses
    .lock()
    .unwrap()
    .clone()
    .into_iter()
    .find(|c| c.teacher_id == teacher_id && c.id == Some(course_id))
    .ok_or("Data Not Found.");

  match selected_course {
    Ok(course) => HttpResponse::Ok().json(course),
    Err(err) => HttpResponse::Ok().json(err),
  }
}
